package com.example.myapplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.Telephony;

import java.util.ArrayList;
import java.util.List;

public class RateManager {
    private DBhelper dBhelper;
    private String TBNAME;
    public RateManager(Context context){
        dBhelper=new DBhelper(context);
        TBNAME=DBhelper.TB_NAME;
    }
    public void add(RateItem item){
        SQLiteDatabase db =dBhelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("curname",item.getCname());
        values.put("currate",item.getCval());
        db.insert(TBNAME,null,values);
        db.close();
    }
    public void addAll(List<RateItem> list){
        SQLiteDatabase db = dBhelper.getWritableDatabase();
        for (RateItem item : list) {
            ContentValues values = new ContentValues();
            values.put("curname", item.getCname());
            values.put("currate", item.getCval());
            db.insert(TBNAME, null, values);
        }
        db.close();
    }
    public List<RateItem> listAll(){
        List<RateItem> rateList = null;
        SQLiteDatabase db = dBhelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, null, null, null, null, null);
        if(cursor!=null){
            rateList = new ArrayList<RateItem>();
            while(cursor.moveToNext()){
                RateItem item = new RateItem();
                int i1 = cursor.getColumnIndex("ID");
                item.setId(cursor.getInt(i1>=0? i1 : 0));
                int i2 =cursor.getColumnIndex("CURNAME");
                item.setCname(cursor.getString(i2>=0? i2 : 0));
                int i3=cursor.getColumnIndex("CURRATE");
                item.setCval(i3>=0? i3 : 0);

                rateList.add(item);
            }
            cursor.close();
        }
        db.close();
        return rateList;

    }
    public void deleteAll(){
        SQLiteDatabase db = dBhelper.getWritableDatabase();
        db.delete(TBNAME,null,null);
        db.close();
    }

}
